package com.jcl.recruit.console.mapper;

import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.console.mapper.provider.ConsoleUserAccessProvider;
import com.jcl.recruit.mapper.SqlMapper;
import com.jcl.recruit.mapper.provider.SqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName ConsoleUserAccessMapper
 * @Author yyliu
 * @Date 2018/12/13 16:45
 * @Version 1.0
 **/
public interface ConsoleUserAccessMapper extends SqlMapper {

    String TABLE_NAME = "t_user_access";

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != statusList\"> and status in ",
            "<foreach collection=\"statusList\" item=\"status\" open=\"(\" close=\")\" separator=\",\"> ",
            " #{status}",
            "</foreach>",
            " </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    List<ConsoleUserAccess> findList(@Param("category") String category,
                                     @Param("statusList") Integer... status);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            "<if test=\"null != externalKey\"> external_key = #{externalKey} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != status\"> and status = #{status} </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    List<ConsoleUserAccess> findListByExternalKey(@Param("externalKey") String externalKey,
                                                  @Param("category") String category,
                                                  @Param("status") Integer status);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            "<if test=\"null != username\"> username = #{username} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != status\"> and status = #{status} </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    List<ConsoleUserAccess> findListByUsername(@Param("username") String username,
                                               @Param("category") String category,
                                               @Param("status") Integer status);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            "<if test=\"null != username\"> username = #{username} </if>",
            "<if test=\"null != password\"> and password = #{password} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != status\"> and status = #{status} </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    ConsoleUserAccess findOneByUsernameAndPw(@Param("username") String username,
                                             @Param("password") String password,
                                             @Param("category") String category,
                                             @Param("status") Integer status);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            "<if test=\"null != externalKey\"> external_key = #{externalKey} </if>",
            "<if test=\"null != corpCode\"> and corp_code = #{corpCode} </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    ConsoleUserAccess findOneByExternalKeyAndCode(@Param("externalKey") String externalKey,
                                                  @Param("corpCode") String corpCode);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            "<if test=\"null != userId\"> user_id = #{userId} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    ConsoleUserAccess findOneByUserId(@Param("userId") Integer userId,
                                      @Param("category") String category);

    @Select({
            SQL_QUERY_ID
    })
    ConsoleUserAccess findOne(@Param("id") Integer id);

    @Update({
            QUERY_SCRIPT_START,
            SQL_UPDATE,
            " set status = #{setStatus}  ",
            QUERY_WHERE_START,
            "<if test=\"null != externalKey\"> and external_key = #{externalKey} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != statuses\"> and status in ",
            "<foreach collection=\"statuses\" item=\"status\" open=\"(\" close=\")\" separator=\",\"> ",
            " #{status}",
            "</foreach>",
            " </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    int changeStatusesToSetting(@Param("setStatus") Integer setStatus,
                                @Param("externalKey") String externalKey,
                                @Param("category") String category,
                                @Param("statuses") Integer... statuses);

    @Update({
            QUERY_SCRIPT_START,
            SQL_UPDATE,
            " set user_id = #{userId} ",
            QUERY_WHERE_START,
            "<if test=\"null != externalKey\"> external_key = #{externalKey} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            QUERY_WHERE_END,
            QUERY_SCRIPT_END
    })
    int updateUserId(@Param("externalKey") String externalKey,
               @Param("userId") Integer userId,
               @Param("category") String category);

    @InsertProvider(type = ConsoleUserAccessProvider.class, method = SqlProvider.SQL_METHOD_INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(ConsoleUserAccess access);

    @UpdateProvider(type = ConsoleUserAccessProvider.class, method = SqlProvider.SQL_METHOD_UPDATE)
    int update(ConsoleUserAccess access);

    String SQL_QUERY = "select * from " + TABLE_NAME + " ";
    String SQL_QUERY_ID = "select * from " + TABLE_NAME + " where id = #{id}";
    String SQL_UPDATE = "update " + TABLE_NAME + " ";
}
